package org.dromara.platform.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.platform.domain.bo.TMiniProgramRecordBo;
import org.dromara.platform.domain.vo.TMiniProgramRecordVo;
import org.dromara.platform.domain.TMiniProgramRecord;
import org.dromara.platform.mapper.TMiniProgramRecordMapper;
import org.dromara.platform.service.ITMiniProgramRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 小程序备案信息Service业务层处理
 *
 * @author Lion Li
 * @date 2025-06-23
 */
@RequiredArgsConstructor
@Service
public class TMiniProgramRecordServiceImpl implements ITMiniProgramRecordService {

    private final TMiniProgramRecordMapper baseMapper;

    /**
     * 查询小程序备案信息
     *
     * @param id 主键
     * @return 小程序备案信息
     */
    @Override
    public TMiniProgramRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询小程序备案信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 小程序备案信息分页列表
     */
    @Override
    public TableDataInfo<TMiniProgramRecordVo> queryPageList(TMiniProgramRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TMiniProgramRecord> lqw = buildQueryWrapper(bo);
        Page<TMiniProgramRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的小程序备案信息列表
     *
     * @param bo 查询条件
     * @return 小程序备案信息列表
     */
    @Override
    public List<TMiniProgramRecordVo> queryList(TMiniProgramRecordBo bo) {
        LambdaQueryWrapper<TMiniProgramRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TMiniProgramRecord> buildQueryWrapper(TMiniProgramRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TMiniProgramRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TMiniProgramRecord::getId);
        lqw.eq(bo.getBaseId() != null, TMiniProgramRecord::getBaseId, bo.getBaseId());
        lqw.eq(bo.getZtxzType() != null, TMiniProgramRecord::getZtxzType, bo.getZtxzType());
        lqw.like(StringUtils.isNotBlank(bo.getZbdwName()), TMiniProgramRecord::getZbdwName, bo.getZbdwName());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), TMiniProgramRecord::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), TMiniProgramRecord::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), TMiniProgramRecord::getDistrict, bo.getDistrict());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), TMiniProgramRecord::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getComment()), TMiniProgramRecord::getComment, bo.getComment());
        lqw.eq(StringUtils.isNotBlank(bo.getRecordNumber()), TMiniProgramRecord::getRecordNumber, bo.getRecordNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getResidencePermit()), TMiniProgramRecord::getResidencePermit, bo.getResidencePermit());
        lqw.eq(bo.getCertificateType() != null, TMiniProgramRecord::getCertificateType, bo.getCertificateType());
        lqw.eq(StringUtils.isNotBlank(bo.getCertificateNumber()), TMiniProgramRecord::getCertificateNumber, bo.getCertificateNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getCertificateAddress()), TMiniProgramRecord::getCertificateAddress, bo.getCertificateAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getCertificatePhoto()), TMiniProgramRecord::getCertificatePhoto, bo.getCertificatePhoto());
        lqw.like(StringUtils.isNotBlank(bo.getFzrName()), TMiniProgramRecord::getFzrName, bo.getFzrName());
        lqw.eq(StringUtils.isNotBlank(bo.getFzrMobile()), TMiniProgramRecord::getFzrMobile, bo.getFzrMobile());
        lqw.eq(StringUtils.isNotBlank(bo.getFzrEmail()), TMiniProgramRecord::getFzrEmail, bo.getFzrEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getEmergencyContact()), TMiniProgramRecord::getEmergencyContact, bo.getEmergencyContact());
        lqw.eq(StringUtils.isNotBlank(bo.getFzrCertificateType()), TMiniProgramRecord::getFzrCertificateType, bo.getFzrCertificateType());
        lqw.eq(StringUtils.isNotBlank(bo.getFzrCertificateNumber()), TMiniProgramRecord::getFzrCertificateNumber, bo.getFzrCertificateNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getCertificateValidityDateStart()), TMiniProgramRecord::getCertificateValidityDateStart, bo.getCertificateValidityDateStart());
        lqw.eq(StringUtils.isNotBlank(bo.getCertificateValidityDateEnd()), TMiniProgramRecord::getCertificateValidityDateEnd, bo.getCertificateValidityDateEnd());
        lqw.eq(StringUtils.isNotBlank(bo.getCertificatePhotoFront()), TMiniProgramRecord::getCertificatePhotoFront, bo.getCertificatePhotoFront());
        lqw.eq(StringUtils.isNotBlank(bo.getCertificatePhotoBack()), TMiniProgramRecord::getCertificatePhotoBack, bo.getCertificatePhotoBack());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthorizationLetter()), TMiniProgramRecord::getAuthorizationLetter, bo.getAuthorizationLetter());
        lqw.eq(StringUtils.isNotBlank(bo.getVerifyTaskId()), TMiniProgramRecord::getVerifyTaskId, bo.getVerifyTaskId());
        lqw.like(StringUtils.isNotBlank(bo.getFrName()), TMiniProgramRecord::getFrName, bo.getFrName());
        lqw.eq(StringUtils.isNotBlank(bo.getFrCertificateNumber()), TMiniProgramRecord::getFrCertificateNumber, bo.getFrCertificateNumber());
        return lqw;
    }

    /**
     * 新增小程序备案信息
     *
     * @param bo 小程序备案信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TMiniProgramRecordBo bo) {
        TMiniProgramRecord add = MapstructUtils.convert(bo, TMiniProgramRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改小程序备案信息
     *
     * @param bo 小程序备案信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TMiniProgramRecordBo bo) {
        TMiniProgramRecord update = MapstructUtils.convert(bo, TMiniProgramRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TMiniProgramRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除小程序备案信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
